<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
		"http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>robot Textarea Test</title>

		<style>
			@import "../../../../util/doh/robot/robot.css";
		</style>

		<!-- required: dojo.js -->
		<script type="text/javascript" src="../../../../dojo/dojo.js"></script>

		<script type="text/javascript">
			require([
				"doh/runner", "dojo/robotx",
				"dojo/dom-geometry", "dojo/keys", "dojo/sniff",
				"dojo/domReady!"
			], function(doh, robot, domGeom, keys, has){
				robot.initRobot('../test_Textarea.html');

				// execute some test as soon as the widget gets focus
				function focusThenRun(widget, fcn){
					if(!widget.focused){
						var handle = widget.on('focus', function(){
							handle.remove();
							setTimeout(fcn, 1);
						});
						widget.focus();
					}else{
						fcn();
					}
				}

				doh.register("init", function init(){
					registry = robot.window.require("dijit/registry");

					// Wait for Textarea widgets to size themselves... it's done on a setTimeout(func, 0)
					var d = new doh.Deferred();
					setTimeout(function(){
						d.callback(true);
					}, 100);
					return d;
				});

				doh.register("height tests", [
					{
						name: "initial height",
						runTest: function(){
							var blank = domGeom.getMarginBox("blank").h,
							large = domGeom.getMarginBox("largeTextArea").h;
							doh.t(blank > 8, "blank: " + blank);
							doh.t(large > blank, "blank: " + blank + ", large: " + large);
						}
					},
					{
						name: "expansion/contraction by newline",
						timeout: 10000,
						runTest: function(){
							var
								d = new doh.Deferred(),
								w = registry.byId("blank"),
								height1 = domGeom.getMarginBox(w.domNode).h,
								height2, height3;

							focusThenRun(w, function(){
								// Test expand on newline
								robot.typeKeys('Row of text.', 1, 2000);
								robot.keyPress(keys.ENTER, 100, {});
								robot.keyPress(keys.ENTER, 100, {});
								robot.sequence(d.getTestErrback(function(){
									height2 = domGeom.getMarginBox(w.domNode).h;
								}), 1000);

								// Test shrink on delete of newline
								robot.keyPress(keys.BACKSPACE, 200, {});
								robot.sequence(d.getTestCallback(function(){
									height3 = domGeom.getMarginBox(w.domNode).h;
									doh.t(height2 > height1, "height ("+height2+") should have increased from " + height1);
									doh.t(height3 < height2, "height ("+height3+") should have decreased from " + height2);
								}), 1000);
							});

							return d;
						}
					},
					{
						name: "expansion/contraction by word wrap",
						timeout: 50000,
						runTest: function(){
							var
								d = new doh.Deferred(),
								w = registry.byId("blank"),
								height1 = domGeom.getMarginBox(w.domNode).h,
								height2,
								height3,
								handler,
								pos,
								i = 0,
								text = 'The quick brown fox jumped over the lazy dog.',
								typing = true;

							w.set('intermediateChanges', true);
							// Test expand by wordwrap
							text = text+text+text+text;
							// onChange handler enables very fast typing and deleting, but not too fast
							handler = w.on('change', function(nv){
								if(typing){
									pos = nv.indexOf(text);
									if(pos <= 0){
										robot.typeKeys(text.substr(i++,1), 1, 0);
										return;
									} // still typing
									height2 = domGeom.getMarginBox(w.domNode).h;
									typing = false;
									// Test shrink on delete (backspace) of text
									robot.keyPress(keys.BACKSPACE, 0, {});
								}else{
									if(nv.length > pos){
										robot.keyPress(keys.BACKSPACE, 0, {});
										return; // still deleting
									}
									handler.remove();
									robot.sequence(d.getTestCallback(function(){
										doh.t(height2 > height1, "height ("+height2+") should have increased from " + height1);
										height3 = domGeom.getMarginBox(w.domNode).h;
										doh.t(height3 < height2, "height ("+height3+") should have decreased from " + height2);
									}), 1);
								}
							});
							robot.typeKeys(text.substr(i++,1), 1, 0);

							return d;
						}
					},
					{
						name: "expansion/contraction by cut/paste",
						timeout: 9000,
						runTest: function(){
							var
								d = new doh.Deferred(),
								modifier = has("mac") ? {meta: true} : {ctrl: true},
								w = registry.byId("blank"),
								height1, height2, height3;

							robot.keyPress(keys.ENTER, 500, {});
							robot.sequence(function(){
								height1 = domGeom.getMarginBox(w.domNode).h;
								w.domNode.select();
							}, 500);
							// Cut text, height should decrease
							robot.keyPress("x", 500, modifier);
							robot.sequence(function(){
								height2 = domGeom.getMarginBox(w.domNode).h;
								doh.t(height2 < height1, "height2 (" + height2 + ") < height1 (" + height1 + ")");
							}, 500);

							// Paste text, height should increase
							robot.keyPress("v", 500, modifier);
							robot.keyPress("v", 500, modifier);
							robot.sequence(d.getTestCallback(function(){
								height3 = domGeom.getMarginBox(w.domNode).h;
								doh.t(height3 > height2, "height3 (" + height3 + ") > height2 (" + height2 + ")");
							}), 500);

							return d;
						}
					}

				]);

				doh.register("maxLength",
					{
						name: "maxLength",
						timeout: 10000,
						runTest: function(){
							var
								d = new doh.Deferred(),
								ta = registry.byId("simple"),
								value1, value2, value3;

							focusThenRun(ta, function(){
								// Limit is 50, and there's text there already, so not all of
								// these characters will fit; some should be rejected.
								robot.typeKeys('0123456789', 1, 2000);
								robot.sequence(function(){
									value1 = ta.get("value");
								}, 500);

								// Erase a character
								robot.keyPress(keys.BACKSPACE, 500, {});
								robot.sequence(function(){
									value2 = ta.get("value");
								}, 500);

								// And try to type a new character, there should be room now,
								// but just for one character
								robot.typeKeys('AB', 500, 400);
								robot.sequence(d.getTestCallback(function(){
									value3 = ta.get("value");
									doh.is(50, value1.length, "should have stopped at 50, value is " + value1);
									doh.is(49, value2.length, "erased a char, value is " + value2);
									doh.is(50, value3.length, "tried to type 2 chars but only 1 should fit, value is " + value3);
								}), 500);
							});

							return d;
						}
					});
				doh.register("min/max height", [
					{
						name: "initial height",
						timeout: 1000,
						runTest: function(){
							var w = registry.byId("programmatic");
							var h = w.domNode.offsetHeight;
							doh.t(h > 64, "initial 3 line height " + h);
						}
					},
					{
						name: "#13166",
						timeout: 5000,
						runTest: function(){
							var
								d = new doh.Deferred(),
								w = registry.byId("programmatic");

							focusThenRun(w, function(){
								var h = w.domNode.offsetHeight;
								robot.keyPress(keys.SPACE, 500, {});
								robot.sequence(d.getTestCallback(function(){
									doh.is(0, w.domNode.scrollTop, "no scroll");
									doh.is(h, w.domNode.offsetHeight, "height should not change");
								}), 500);
							});

							return d;
						}
					},
					{
						name: "less than max-height",
						timeout: 5000,
						runTest: function(){
							var
								d = new doh.Deferred(),
								w = registry.byId("programmatic");

							var h = w.domNode.offsetHeight;
							w.set('value', '');
							doh.t(h > w.domNode.offsetHeight, "min-height is smaller than 3 lines: " + w.domNode.offsetHeight);
							doh.t(40 < w.domNode.offsetHeight, "min-height is at least 1 line: " + w.domNode.offsetHeight);
							robot.keyPress(keys.ENTER, 500, {});
							robot.keyPress(keys.ENTER, 500, {});
							robot.keyPress(keys.ENTER, 500, {});
							robot.sequence(d.getTestCallback(function(){
								doh.is(0, w.domNode.scrollTop, "no scroll");
								doh.t(h < w.domNode.offsetHeight, "height grew " + w.domNode.offsetHeight);
								if(has("ie") <= 7){
									doh.t(w.domNode.parentNode.parentNode.clientHeight >= w.domNode.parentNode.parentNode.scrollHeight, "non-scrolled parent");
								}
							}), 500);

							return d;
						}
					},
					{
						name: "more than max-height",
						timeout: 5000,
						runTest: function(){
							var
								d = new doh.Deferred(),
								w = registry.byId("programmatic");

							var h = w.domNode.offsetHeight;
							robot.keyPress(keys.ENTER, 500, {});
							robot.keyPress(keys.ENTER, 500, {});
							robot.keyPress(keys.ENTER, 500, {});
							robot.keyPress(keys.ENTER, 500, {});
							robot.keyPress(keys.ENTER, 500, {});
							robot.sequence(d.getTestCallback(function(){
								if(has("ie") <= 6){
									doh.is(0, w.domNode.scrollTop, "no scroll");
									doh.t(w.domNode.parentNode.parentNode.clientHeight < w.domNode.parentNode.parentNode.scrollHeight, "scrolled parent");
								}else{
									doh.isNot(0, w.domNode.scrollTop, "scroll");
								}
								doh.t(h < w.domNode.offsetHeight, "height grew " + w.domNode.offsetHeight);
							}), 500);

							return d;
						}
					},
					{
						name: "delete until less than max-height",
						timeout: 5000,
						runTest: function(){
							var
								d = new doh.Deferred(),
								w = registry.byId("programmatic");

							var h = w.domNode.offsetHeight;
							robot.keyPress(keys.BACKSPACE, 500, {});
							robot.keyPress(keys.BACKSPACE, 500, {});
							robot.keyPress(keys.BACKSPACE, 500, {});
							robot.keyPress(keys.BACKSPACE, 500, {});
							robot.keyPress(keys.BACKSPACE, 500, {});
							robot.sequence(d.getTestCallback(function(){
								doh.is(0, w.domNode.scrollTop, "no scroll");
								if(has("ie") <= 7){
									doh.t(w.domNode.parentNode.parentNode.clientHeight >= w.domNode.parentNode.parentNode.scrollHeight, "non-scrolled parent");
								}
								doh.t(h > w.domNode.offsetHeight, "height shrank " + w.domNode.offsetHeight);
							}), 500);

							return d;
						}
					}

				]);

				doh.run();
			});
		</script>
	</head>
</html>
